package com.tang.client;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;
import io.netty.util.concurrent.Future;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * 监听类
 *
 * @author fulin.tang
 * @version V3.0
 * @since 2022/5/20 18:10
 */
@Slf4j
public class ClientChannelFutureListener implements ChannelFutureListener {

    /**
     * 连接上了之后就会监听到消息哦
     *
     * @param future the source {@link Future} which called this callback
     */
    @Override
    public void operationComplete(ChannelFuture future) {
        if (future.isSuccess()) {
            log.info("netty client start done!");
            return;
        }
        final EventLoop loop = future.channel().eventLoop();
        loop.schedule(() -> {
            try {
                new NettyClient().connect("127.0.0.1", 7397);
                log.info("netty client reconnect done!");
                Thread.sleep(500);
            } catch (Exception e) {
                log.error("netty client start error go reconnect!");
            }
        }, 1L, TimeUnit.SECONDS);
    }

}
